package com.housekeeping.controller.api;

import com.housekeeping.utils.ApiResponse;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统管理模块控制器
 */
@WebServlet("/api/system/*")
public class SystemApiController extends HttpServlet {
    private UserService userService = new UserServiceImpl();
    private OrderService orderService = new OrderServiceImpl();
    private HousekeepingServiceService serviceService = new HousekeepingServiceServiceImpl();
    private ReviewService reviewService = new ReviewServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        String pathInfo = req.getPathInfo();
        try {
            if (pathInfo != null && pathInfo.equals("/dashboard")) {
                handleGetDashboard(req, resp);
            } else if (pathInfo != null && pathInfo.equals("/stats")) {
                handleGetStats(req, resp);
            } else {
                ApiResponse.sendJsonResponse(req, resp, ApiResponse.error(404, "Not Found"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            ApiResponse.sendJsonResponse(req, resp, ApiResponse.error(500, "Internal Error"));
        }
    }

    private void handleGetDashboard(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            ApiResponse.sendJsonResponse(req, resp, ApiResponse.error(401, "Not logged in"));
            return;
        }
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null) {
            ApiResponse.sendJsonResponse(req, resp, ApiResponse.error(401, "Not logged in"));
            return;
        }
        Map<String, Object> dashboardData = new HashMap<>();
        dashboardData.put("totalUsers", userService.getUserCount());
        dashboardData.put("totalOrders", orderService.getOrderCount());
        dashboardData.put("totalServices", serviceService.getServiceCount());
        dashboardData.put("totalReviews", reviewService.getReviewCount());
        ApiResponse.sendJsonResponse(req, resp, ApiResponse.success(dashboardData));
    }

    private void handleGetStats(HttpServletRequest req, HttpServletResponse resp)
            throws IOException {
        HttpSession session = req.getSession(false);
        if (session == null) {
            ApiResponse.sendJsonResponse(req, resp, ApiResponse.error(401, "Not logged in"));
            return;
        }
        User currentUser = (User) session.getAttribute("user");
        if (currentUser == null) {
            ApiResponse.sendJsonResponse(req, resp, ApiResponse.error(401, "Not logged in"));
            return;
        }

        Map<String, Object> statsData = new HashMap<>();
        statsData.put("userCount", userService.getUserCount());
        statsData.put("orderCount", orderService.getOrderCount());
        statsData.put("serviceCount", serviceService.getServiceCount());
        statsData.put("reviewCount", reviewService.getReviewCount());
        ApiResponse.sendJsonResponse(req, resp, ApiResponse.success(statsData));
    }


} 